package com.zzyl.nursing.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.common.utils.http.HttpUtils;
import com.zzyl.nursing.service.WechatService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.Map;


@Service
@Slf4j
public class WechatServiceImpl implements WechatService {


    // 登录
    private static final String openIDUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL =  "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    // 获取手机号
    private static final String PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN";


    @Value("${wechat.appId}")
    private String appid;

    @Value("${wechat.appSecret}")
    private String secret;


    /**
     * 获取openid
     * @param code
     * @return
     */
    @Override
    public String getOpenid(String code) {
        try {
            String url =openIDUrl.replace("APPID", appid).replace("SECRET", secret).replace("JSCODE", code);
            String result = HttpUtil.get(url);
            JSONObject jsonObject = JSON.parseObject(result);
            String openid = jsonObject.getString("openid");
            return openid;
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("获取openid失败");
        }
    }

    /**
     * 获取手机号
     * @param detailCode
     * @return
     */
    @Override
    public String getPhone(String detailCode) {
        try {
        String url = PHONE_URL.replace("ACCESS_TOKEN", getToken());
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("code", detailCode);
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramMap));
        log.info("获取微信手机号结果",result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
        if (phoneInfo!=null){
            String phoneNumber = phoneInfo.getString("phoneNumber");
            return phoneNumber;
        }
        throw new ServiceException("获取手机号失败");
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("获取手机号失败");
        }
    }

    /**
     * 获取token
     * @return
     */
    private String getToken() {
        String url = TOKEN_URL.replace("APPID", appid).replace("APPSECRET", secret);
        String result = HttpUtils.sendGet(url);
        JSONObject jsonObject = JSON.parseObject(result);
        log.info("获取微信token结果",result);
        String token = jsonObject.getString("access_token");
        return token;
    }
}